package com.gw.quotes.adapter;

import java.util.ArrayList;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.gw.quotes.R;
import com.gw.quotes.entity.business.Pricelist;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.FadeInBitmapDisplayer;
import com.yxw.view.util.ResFinder;

public class PriceListAdapter extends BaseAdapter {

	private Context context;
	private int resource;
	private ArrayList<Pricelist> list;
	private DisplayImageOptions options;
	protected ImageLoader imageLoader;

	public PriceListAdapter(Context context, int layoutId,
			ArrayList<Pricelist> list) {
		this.context = context;
		this.resource = layoutId;
		this.list = list;
		this.imageLoader = ImageLoader.getInstance();
		this.imageLoader.init(ImageLoaderConfiguration.createDefault(context));
		this.options = new DisplayImageOptions.Builder()
				.showImageOnLoading(R.drawable.ic_empty)
				.showImageForEmptyUri(
						ResFinder.getR(context, "R.drawable.ic_empty"))
				.showImageOnFail(ResFinder.getR(context, "R.drawable.ic_empty"))
				.cacheInMemory(true).cacheOnDisk(true)
				.displayer(new FadeInBitmapDisplayer(500))// 是否图片加载好后渐入的动画时间
				.bitmapConfig(Bitmap.Config.RGB_565).build();
	}

	@Override
	public int getCount() {
		return this.list.size();
	}

	@Override
	public Object getItem(int position) {
		return this.list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Pricelist priceitem = list.get(position);
		if (convertView == null) {
			convertView = LayoutInflater.from(this.context).inflate(
					this.resource, parent, false);
		}
		TextView goodsname = (TextView) convertView
				.findViewById(R.id.search_title);
		TextView agreementprice = (TextView) convertView
				.findViewById(R.id.search_price);
		ImageView goods_img = (ImageView) convertView
				.findViewById(R.id.search_img);
		agreementprice.setText(String.format("￥%.2f",
				priceitem.getAgreementprice()));
		goodsname.setText(priceitem.getGoodsname());
		this.imageLoader.displayImage(priceitem.getImgurl(), goods_img,
				this.options);
		return convertView;
	}

}
